/********************************************************************************************************
* File : C8051F330_PortInit
*********************************************************************************************************/
#ifndef _C8051F330_PORT_INIT_H_
#define _C8051F330_PORT_INIT_H_

#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif

//The fellowed are added by lei
#define SYSCLK      24500000           // SYSCLK frequency in Hz
#define BAUDRATE        9600           // Baud rate of UART in bps
// add end

// 16-bit SFR declarations
sfr16 TMR3RL = 0x92; // Timer3 reload registers
sfr16 TMR3 = 0x94; // Timer3 counter registers

void fn_C8051F330_SysClock_Init(void)
{
   OSCICN |= 0x03;                     // Configure internal oscillator for
                                       // its maximum frequency
   RSTSRC  = 0x04;                     // Enable missing clock detector
}

// Configure the Crossbar and GPIO ports.
// P0.4 - UART TX
// P0.5 - UART RX
// P1.3 - LED
void fn_C8051F330_Port_Init (void)
{
//P0MDIN's default value is 0xff(Digital Port).So the follow line can be ignored.
	P0MDIN  |= 0x03;					// enable P0.0(SDA),P0.1(SCL) as Digital Port. 
	P0MDOUT |= 0x10;                    // enable P0.4(UTX) as push-pull output

//P1MDIN's default value is 0xff(Digital Port).So the follow line can be ignored.
//	P1MDIN  |= 0x03;					// enable P1.0,P1.1 as Digital Port
//	P1MDOUT |= 0x03;					// enable P1.0,P1.1 as push-pull output

//	P1MDIN  |= 0x07;					// enable P1.0,P1.1 as Digital Port
	P1MDIN  |= 0x82;					// enable P1.7(DATA),P1.1(CLK) as Digital Port
	P1MDIN  |= 0x01;					// enable P1.0(RS485Ctrl) as Digital Port

	P1MDOUT |= 0x01;						// enable P1.0(RS485Ctrl) as push-pull output

	XBR0 |= 0x01;                    	// Enable UART on P0.4(TX) and P0.5(RX)                     
	XBR0 |= 0x04;						// Enable SMBus on P0.0(SDA),P0.1(SCL)
	XBR1 |= 0x40;                    	// Enable crossbar and weak pull-ups
}

//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
void fn_C8051F330_Uart0_Init (void)
{
	SCON0 = 0x10;						// SCON0: 8-bit variable bit rate
										//        level of STOP bit is ignored
										//        RX enabled
										//        ninth bits are zeros
										//        clear RI0 and TI0 bits
	if (SYSCLK/BAUDRATE/2/256 < 1) {
      TH1 = -(SYSCLK/BAUDRATE/2);
      CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
      CKCON |=  0x08;
   } else if (SYSCLK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSCLK/BAUDRATE/2/4);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01                  
      CKCON |=  0x09;
   } else if (SYSCLK/BAUDRATE/2/256 < 12) {
      TH1 = -(SYSCLK/BAUDRATE/2/12);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
   } else {
      TH1 = -(SYSCLK/BAUDRATE/2/48);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
      CKCON |=  0x02;
   }
	TL1 = TH1;                          		// init Timer1
	TMOD &= ~0xf0;                      		// TMOD: timer 1 in 8-bit autoreload
	TMOD |=  0x20;                       
	TR1 = 1;                            		// START Timer1
	TI0 = 1;                            		// Indicate TX0 ready

	ES0 = 1;				//lei added

}

void SMBus_Init(void) 
{
	SMB0CF = 0x1D; 		

//	SMB0CF = 0x5D; 		
						// Bit1-0=SMBCS1-SMBCS0:Use Timer1 overflows as SMBus clock source;
						// Bit2=SMBFTE:Enable SMBus Free timeout detect;
						// Bit3=SMBTOE:Enable SCL low timeout detect;
						// Bit4=EXTHOLD:Enable setup & hold time extensions;
						// Bit5=BUSY:(only read)
						// Bit6=INH:Disable slave mode;0:enable and 1:disable
						// Bit7=ENSMB:

	SMB0CF |= 0x80; 	// Enable SMBus 
}



void fn_C8051F330_Interrupt_Enable(void)
{
//	EIE1 |= 0x80; 					// Timer3 interrupt enable
	EIE1 |= 0x01; 					// Enable the SMBus interrupt
	EA = 1; 						// Global interrupt enable
}

void fn_C8051F330_Init(void)
{
	PCA0MD &= ~0x40;				// WDTE = 0 (Disable watchdog timer)
	fn_C8051F330_SysClock_Init();
	fn_C8051F330_Port_Init();
	fn_C8051F330_Uart0_Init();
	SMBus_Init();
	fn_C8051F330_Interrupt_Enable();
}
#endif /*_C8051F330_PORT_INIT_H_*/